package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.GuestService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/guest")
public class GuestController {
    @Autowired
    GuestService guestService;

    //分页查询和模糊查询
    @RequestMapping("/guest-list")
    @ResponseBody
    public Map<String,Object> GuestList(String gLianxir,String gLxfshi,String gGongsi,
                                        String gXuqiu,String gBeizhu, String gState,String gRizhi,Integer pageNum, Integer pageSize,HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        List<Guest> guestList = guestService.allGuests(gLianxir,gLxfshi,gGongsi,gBeizhu,gXuqiu,gState,gRizhi,empId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",guestList);
        return map;
    }

    //按id查询
    @RequestMapping("/xqguest-list")
    @ResponseBody
    public Guest xqGuests(Integer gId){
        return guestService.xqGuests(gId);
    }

    //新增
    @RequestMapping("/add-guest")
    @ResponseBody
    public void addGuest(@RequestBody Guest guest, HttpSession session){
        guestService.addGuest(guest,session);
    }

    //修改获客日志
    @RequestMapping("/xiugai-guest")
    @ResponseBody
    public  void xiugaiGuest(Integer gId,String leixing){
        guestService.xiugaiGuest(gId,leixing);
    }

    //删除
    @RequestMapping("/delete-guest")
    @ResponseBody
    public void deleteGuest(int id){
        guestService.deleteGuest(id);
    }
}
